package ym.service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import ym.mapper.MealPlanMapper;
import ym.pojo.MealPlan;
import ym.service.MealPlanService;

import java.util.List;

@DubboService
public class MealPlanServiceImpl implements MealPlanService {
    @Autowired
    private MealPlanMapper mealPlanMapper;

    @Override
    public void add(MealPlan mealPlan) {
        mealPlanMapper.insert(mealPlan);
    }

    @Override
    public void update(MealPlan mealPlan) {
        mealPlanMapper.updateById(mealPlan);
    }

    @Override
    public void delete(Integer id) {
        mealPlanMapper.deleteById(id);
    }

    @Override
    public void deleteByNutritionPlanId(Integer nutritionPlanId) {
        QueryWrapper<MealPlan> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("nutritionPlanId", nutritionPlanId);
        mealPlanMapper.delete(queryWrapper);
    }

    @Override
    public MealPlan findById(Integer id) {
        return mealPlanMapper.selectById(id);
    }

    @Override
    public List<MealPlan> findAll() {
        return mealPlanMapper.selectList(null);
    }

    @Override
    public Page<MealPlan> search(String search, int page, int size) {
        QueryWrapper<MealPlan> queryWrapper = new QueryWrapper<>();
        if (search != null) {

        }
        return mealPlanMapper.selectPage(new Page(page, size), queryWrapper);
    }

    @Override
    public Page<MealPlan> searchByNutritionPlanId(Integer nutritionPlanId, String search, int page, int size) {
        QueryWrapper<MealPlan> queryWrapper = new QueryWrapper<>();
        if (search != null) {
            queryWrapper.eq("nutritionPlanId", nutritionPlanId);
        } else {
            queryWrapper.eq("nutritionPlanId", nutritionPlanId);
        }
        return mealPlanMapper.selectPage(new Page(page, size), queryWrapper);
    }

}
